package com.sz.biz.common.base.service.impl;

import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.common.base.entity.SettlementType;
import com.sz.biz.common.base.service.SettlementTypeService;
import com.sz.common.base.cache.EntityCache;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.core.service.PrincipalLogUtils;
import com.sz.common.core.service.aop.JudgeAspect;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function: 结算service impl <br>
 * Author: hpeng.wang <br>
 * Date: 2016-11-25 11:23:00
 */
@Service
public class SettlementTypeServiceImpl extends AbstractService implements SettlementTypeService {

    @Override
    protected String getMapperNamespace() {
        return "SettlementTypeMapper";
    }


    @Override
    public void refreshCache() {
        EntityCache.clearNamespace(getMapperNamespace());
    }


    @JudgeAspect
    @Override
    public int save(SettlementType entry) {
        dao.save(getSqlName("insertSelective"), entry);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_SETTLEMENT_TYPE", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_SETTLEMENT_TYPE, UserActions.ADD, "新增结算类型", dataBackup);
        return entry.getId();
    }


    @Override
    public int delete(Integer id) {
        SettlementType entry = this.findById(id);
        if (entry == null) {
            throw Exceptions.bizException(ErrorCodes.ERROR_OBJECT_EMPTY);
        }
        if(entry.getIsSystem()){
            throw Exceptions.bizException(ErrorCodes.ERROR_MODEL_ISSYSTEM_NODELETE);
        }
        entry.setIsDeleted(true);
        entry.setLastUpdateTime(new Date());
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_SETTLEMENT_TYPE", id);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_SETTLEMENT_TYPE, UserActions.DELETE, "删除结算类型", dataBackup);
        return this.update(entry);
    }

    @Override
    public void batchdelete(List<Integer> ids) {
        List<SettlementType> list = findByIds(ids);
        if (list.size() > 0) {
            for (SettlementType entry : list) {
                if(entry.getIsSystem()){
                    throw Exceptions.bizException(ErrorCodes.ERROR_MODEL_ISSYSTEM_NODELETE);
                }
                entry.setIsDeleted(true);
                entry.setLastUpdateTime(new Date());
            }
            batchUpdate(list);
            //保存操作日志
            Map<String, Object> dataBackup = new HashMap<>();
            dataBackup.put("DELETE_SETTLEMENT_TYPE", ids);
            PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_SETTLEMENT_TYPE, UserActions.DELETE, "删除结算类型", dataBackup);
        }
    }

    @JudgeAspect
    @Override
    public int update(SettlementType entry) {
        SettlementType entryFind = this.findById(entry.getId());
        if(entryFind!=null && entry.getIsSystem()){
            throw Exceptions.bizException(ErrorCodes.ERROR_MODEL_ISSYSTEM_NOEDIT);
        }
        int result = (int) dao.update(getSqlName("updateByPrimaryKey"), entry);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("UPDATE_SETTLEMENT_TYPE", entry);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_SETTLEMENT_TYPE, UserActions.UPDATE, "修改结算类型", dataBackup);
        return result;
    }

    @Override
    public void batchUpdate(List<SettlementType> list) {
        //数据处理
        dao.batchUpdate(getSqlName("updateByPrimaryKey"), list);
    }

    @Override
    public SettlementType findById(Integer id) {
        ParamData pd = new ParamData();
        pd.put("id", id);
        SettlementType settlementType = (SettlementType) dao.findForObject
                (getSqlName("findById"), pd);
        return settlementType;
    }


    @Override
    public List<SettlementType> findByIds(List<Integer> ids) {
        if (ids == null || ids.size() <= 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "ids");
        }
        ParamData pd = new ParamData();
        pd.put("ids", ids);
        pd.put("isDeleted", false);
        List<SettlementType> list = dao.findForList(getSqlName("findByIds"), pd, SettlementType
                .class);
        return list;
    }

    @Override
    public String findNameByCode(String code) {
        String name = null;
        if (StringUtils.hasLength(code)) {
            name = (String) dao.findForObject(getSqlName("findNameByCode"), code);
        }
        return name;
    }

    @Override
    public QResultDto findByParam(QueryParams params) {
        ParamData pd = this.convertQueryParams(params);
        List<SettlementType> list = dao.findForList(getSqlName("findBySearchListPage"), pd,
                SettlementType.class);
        return new QResultDto(list, pd.getPagination());
    }

    @Override
    public QResultDto findI18nByParam(QueryParams params) {
        ParamData pd = this.convertQueryParams(params);
        List list = dao.findForList(getSqlName("findI18nByParamListPage"), pd, Map.class);
        return new QResultDto(list, pd.getPagination());
    }

    @Override
    public Map<Integer, String> findMapByIdList(List<Integer> settlementTypeIdList) {
        Map<Integer, String> settlementTypeMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(settlementTypeIdList)) {
            ParamData pd = new ParamData();
            pd.put("ids", settlementTypeIdList);
            List<SettlementType> settlementTypeList = dao.findForList(getSqlName("findByIdList"), pd, SettlementType.class);
            for (SettlementType settlementType : settlementTypeList) {
                settlementTypeMap.put(settlementType.getId(), settlementType.getName());
            }
        }

        return settlementTypeMap;
    }
}
